import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("../views/index.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login.vue"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../views/register.vue"),
    },
  ],
});
router.beforeEach(async (to, from, next) => {
  var auth = localStorage.getItem("token");
  if (to.path == "/login" || to.path == "/register") {
    if (auth) {
      router.push("/");
    } else {
      next();
    }
  } else {
    if (auth) {
      next();
    } else {
      router.push("/login");
    }
  }
});
export default router;
